package com.liuyang.service.impl;

import cn.hutool.core.map.MapBuilder;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liuyang.entity.Logs;
import com.liuyang.mapper.LogsMapper;
import com.liuyang.service.LogsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 作者:caleb
 * @since 2022-03-17
 */
@Service
public class LogsServiceImpl extends ServiceImpl<LogsMapper, Logs> implements LogsService {

    @Autowired
    private LogsMapper logsMapper;

    @Override
    public Map<String, Object> queryLogsInfo(String startTime, String endTime, int page, int limit) {
        QueryWrapper<Logs> queryWrapper = new QueryWrapper<Logs>();
        if(!StrUtil.isEmpty(startTime) && !StrUtil.isEmpty(endTime)) {
            queryWrapper.between("logTime", startTime, endTime);
        }
        Page<Logs> logsPage = new Page<>(page, limit);
        IPage<Logs> logsIPage = logsMapper.selectPage(logsPage, queryWrapper);
        long count = logsIPage.getTotal();
        List<Logs> list = logsIPage.getRecords();
        MapBuilder<String, Object> mapBuilder = MapBuilder.create();
        Map<String, Object> map = mapBuilder.put("count", count).put("list", list).build();
        return map;
    }
}
